package uc108.com.jetpackdemo.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import uc108.com.jetpackdemo.R;
import uc108.com.jetpackdemo.bean.SelectInfo;
import uc108.com.jetpackdemo.viewmodel.SelectInfoViewModel;

public class RightFragment extends Fragment {
    TextView tvContent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        SelectInfoViewModel selectInfoViewModel = SelectInfoViewModel.getInstance(this);
        SelectInfoViewModel selectInfoViewModel = ViewModelProviders.of(getActivity()).get(SelectInfoViewModel.class);
        selectInfoViewModel.getData().observe(this,
                new Observer<SelectInfo>() {
                    @Override
                    public void onChanged(SelectInfo selectInfo) {
                        tvContent.setText(String.format("当前选择的是第%d个按钮，\n文字内容是:%s",
                                selectInfo.index, selectInfo.text));
                        tvContent.setTextSize(selectInfo.textSize / 2);
                        tvContent.setTextColor(selectInfo.color);
                    }
                });
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_right, container, false);
        return v;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvContent = view.findViewById(R.id.tvcontent);
    }
}
